package com.aitou.mrylive;

import android.app.Application;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.multidex.MultiDexApplication;

import com.baidu.location.service.LocationService;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.core.ImagePipelineConfig;
import com.facebook.imagepipeline.listener.RequestListener;
import com.facebook.imagepipeline.listener.RequestLoggingListener;
import com.pili.pldroid.streaming.StreamingEnv;
import com.tencent.bugly.crashreport.CrashReport;
import com.umeng.analytics.MobclickAgent;
import com.aitou.mrylive.data.sharedpreference.PrefsHelper;
import com.aitou.mrylive.presentation.ui.chatting.utils.SharePreferenceManager;
import com.yolanda.nohttp.Logger;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.RequestQueue;

import java.util.HashSet;
import java.util.Set;

import cn.jpush.im.android.api.JMessageClient;
import cn.sharesdk.framework.ShareSDK;
/**
 * @since 1.0.0
 */
public class BeautyLiveApplication extends MultiDexApplication {

//    private static final String LOG_TAG = BeautyLiveApplication.class.getSimpleName();
    private static final String JCHAT_CONFIGS = "JChat_configs";
    public static final String TARGET_ID = "targetId";
    public static final String TARGET_APP_KEY = "targetAppKey";
    public static final String DRAFT = "draft";
    public static String PICTURE_DIR = "sdcard/xingmoxiu/pictures/";
    public static final int REQUEST_CODE_CROP_PICTURE = 18;
    private static Context applicationContext;
    public LocationService locationService;
    private static RequestQueue requestQueue;

    public static RequestQueue getRequestQueue(){
        return requestQueue;
    }

    @Override
    public void onCreate() {
        super.onCreate();
//        MobclickAgent.setDebugMode(true);
//        MobclickAgent.setScenarioType(this, MobclickAgent.EScenarioType.E_UM_GAME);
//        MobclickAgent.openActivityDurationTrack(false);
        


        //初始化JMessage-sdk
        JMessageClient.init(this);
        //设置Notification的模式
        JMessageClient.setNotificationMode(JMessageClient.NOTI_MODE_NO_NOTIFICATION);
        //初始化SharePreference
        SharePreferenceManager.init(this, JCHAT_CONFIGS);

        NoHttp.init(this);
        Logger.setDebug(true);
        requestQueue= NoHttp.newRequestQueue();

        applicationContext = this;
        //初始化Bugly
        CrashReport.initCrashReport(getApplicationContext(), getString(R.string.bugly_app_key), true);
        //初始化Prefs
        PrefsHelper.init(this);

        //初始化shareSDK
        ShareSDK.initSDK(this);
        
        //初始化Fresco
        Set<RequestListener> listeners = new HashSet<>();
        listeners.add(new RequestLoggingListener());
        ImagePipelineConfig config = ImagePipelineConfig.newBuilder(this)
                .setRequestListeners(listeners)
                .setDownsampleEnabled(true)
                .setBitmapsConfig(Bitmap.Config.RGB_565)
                .build();
        Fresco.initialize(this, config);

        //初始化七牛视频服务
        StreamingEnv.init(getApplicationContext());
        /***
         * 初始化定位sdk，建议在Application中创建
         */
        locationService = new LocationService(getApplicationContext());
//        doAsyncInitWorks();
    }
//
//    private void doAsyncInitWorks() {
//        Observable.just("stub").subscribeOn(Schedulers.computation())
//                .doOnNext(new Action1<String>() {
//                    @Override
//                    public void call(String s) {
//                        L.v(false, LOG_TAG, "executing async tasks....");
//                        executeAsyncTasks();
//                    }
//                }).subscribe();
//    }
//
//    //Put all async init task here
//    private void executeAsyncTasks() {
//        startService(WebSocketService.createIntent(this));
//    }

    /**
     * Only call this method when have to call this!
     */
    public static Context getContextInstance() {
        return applicationContext;
    }

    public static void setPicturePath(String appKey) {
        if (!SharePreferenceManager.getCachedAppKey().equals(appKey)) {
            SharePreferenceManager.setCachedAppKey(appKey);
            PICTURE_DIR = "sdcard/JChat/pictures/" + appKey + "/";
        }
    }
}
